﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using BLL;
using DAL;
using Models;
using System.Data;
using System.Text;
using System.Xml;
using Gift.WebApp;

public partial class backstage_login : System.Web.UI.Page
{
    // Fields
    protected AdminManager am = new AdminManager();

    protected DataEncryption de = new DataEncryption();


    protected string webSiteXmlPath = "/cn/xml/website_set.xml";
    protected string xmlPath = "/xml/language_version.xml";

    // Methods
    protected StringBuilder BasicBind()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            XmlDocument document = new XmlDocument();
            document.Load(base.Server.MapPath(this.webSiteXmlPath));
            XmlElement element = document.SelectSingleNode("set/cn_enterprise_name") as XmlElement;
            builder.AppendLine("<p style=\"margin:192px 0 0 100px;\">企业名称：<strong>" + element.GetAttribute("value") + "</strong></p>");
            XmlElement element2 = document.SelectSingleNode("set/telephone") as XmlElement;
            builder.AppendLine("<p>联系电话：" + element2.GetAttribute("value") + "</p>");
            XmlElement element3 = document.SelectSingleNode("set/domain_web_site") as XmlElement;
            builder.AppendLine("<p>域名网址：<a target=\"_blank\" href=\"" + element3.GetAttribute("value") + "\">" + element3.GetAttribute("value") + "</a></p>");
        }
        catch
        {
        }
        return builder;
    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        if (this.Page.IsValid)
        {
            Admin admin = new Admin();
            admin.UserName = this.txtLoginName.Value;
            admin.UserPwd = this.txtPwssword.Value;
            admin.LastLoginIP = new ClinetMAC().GetIPAddress();
            DataTable adminByUserName = this.am.GetAdminByUserName(admin.UserName);
            if (adminByUserName.Rows.Count > 0)
            {
                DataRow row = adminByUserName.Rows[0];
                admin.Id = Convert.ToInt32(row["id"]);
                admin.GroupId = Convert.ToInt32(row["adminGroup"]);
                admin.Name = row["name"].ToString();
            }
            this.am.ModifyAdminLoginInfo(admin);
            this.Session["admin_user"] = admin;
            this.Session["language_version"] = base.Request.Form["languageVersion"];
            base.Response.Redirect("index.html");
        }
    }

    protected void cvCode_ServerValidate(object source, ServerValidateEventArgs args)
    {
        args.IsValid = true;
        if (this.Session["alCheckCode"] != null)
        {
            if (string.Compare(this.txtCode.Value, this.Session["alCheckCode"].ToString(), true) != 0)
            {
                args.IsValid = false;
            }
        }
        else
        {
            args.IsValid = false;
        }
    }

    protected void cvLoginNameAndPassword_ServerValidate(object source, ServerValidateEventArgs args)
    {
        args.IsValid = true;
        Admin admin = new Admin();
        admin.UserName = this.txtLoginName.Value;
        admin.UserPwd = Gift.Security.DESEncrypt.Encrypt(this.txtPwssword.Value);
        if (!this.am.AdminLogin(admin))
        {
            args.IsValid = false;
        }
    }

    protected StringBuilder FooterBind()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            XmlDocument document = new XmlDocument();
            document.Load(base.Server.MapPath(this.webSiteXmlPath));
            XmlElement element = document.SelectSingleNode("set/copyright") as XmlElement;
            XmlElement element2 = document.SelectSingleNode("set/cn_enterprise_sort_name") as XmlElement;
            XmlElement element3 = document.SelectSingleNode("set/domain_web_site") as XmlElement;
            builder.AppendLine("<p class=\"copyright\">" + element.GetAttribute("value") + "&nbsp;&nbsp;" + element2.GetAttribute("value") + "&nbsp;<a target=\"_blank\" href=\"" + element3.GetAttribute("value") + "\">" + element3.GetAttribute("value") + "</a></p>");
        }
        catch
        {
        }
        return builder;
    }

    protected StringBuilder GetLanguage()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            XmlDocument document = new XmlDocument();
            document.Load(base.Server.MapPath(this.xmlPath));
            XmlNodeList childNodes = document.SelectSingleNode("language").ChildNodes;
            foreach (XmlNode node in childNodes)
            {
                XmlElement element = (XmlElement)node;
                builder.AppendLine("<option value=\"" + element.GetAttribute("code") + "\">" + element.GetAttribute("name") + "</option>");
            }
        }
        catch
        {
        }
        return builder;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!base.IsPostBack)
        {
            this.txtLoginName.Focus();
        }
    }

}